浏览量 4581
2019/01/08 11:48
子向父组件传值调用顺序:
header.vue子组件调用:
methods: {
changetitle: function () {
// this.title1 = 'changed';
this.$emit("titleChanged","子to父组件传值");
}
}
去父组件app.vue找titleChanged:
<app-header v-on:titleChanged="updatetitle($event)" v-bind:title1="title1"></app-header>
父组件实现方法:
methods:{
updatetitle(title){
this.title1=title;
}
},
通过bind影响header和footer的title:
<app-header v-on:titleChanged="updatetitle($event)" v-bind:title1="title1"></app-header>
<app-footer v-bind:title1="title1"></app-footer>
Header.vue
<template>
<header v-on:click="changetitle">
<h1>{{title}}{{title1}}</h1>
</header>
</template>
<script>
export default {
name: 'app-header',
props: {
title1: {
type: String
}
},
data() {
return {
title: 'Vue.js Demo'
}
},
methods: {
changetitle: function () {
// this.title1 = 'changed';
this.$emit("titleChanged","子to父组件传值");
}
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
header {
background: lightgreen;
padding: 10px;
}
h1 {
color: #222;
text-align: center;
}
</style>
App.vue
<!-- 1模板:html结构 -->
<template>
<div id="app">
<app-header v-on:titleChanged="updatetitle($event)" v-bind:title1="title1"></app-header>
<users v-bind:users="users"></users>
<users v-bind:users="users"></users>
<app-footer v-bind:title1="title1"></app-footer>
</div>
</template>
<!-- 2行为:逻辑处理 -->
<script>
// 局部注册组件
import Users from './components/Users'
import Header from './components/Header'
import Footer from './components/Footer'
export default {
name: 'App',
data() {
return {
// title: "这是一个干净的脚手架项目!"
users:[
{name:"brownwang",position:"运维开发",show:false},
{name:"brownwang",position:"运维开发",show:false},
{name:"brownwang",position:"运维开发",show:false},
{name:"brownwang",position:"运维开发",show:false}],
title1:"传递的是一个值 number string boolean"
}
},
methods:{
updatetitle(title){
this.title1=title;
}
},
components: {
"users": Users,
"app-header": Header,
"app-footer": Footer
}
}
</script>
<!-- 3样式:解决样式 -->
<style scoped>
h1 {
color: purple;
}
</style>